﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ListenMusic.AdminAlbum_ServiceReference;
using ListenMusic.Models.Reposistory.Public;
using ListenMusic.Models.Reposistory.User;
using System.Transactions;
using System.IO;
using ListenMusic.Singer_ServiceReference;
using ListenMusic.Musician_ServiceReference;
using System.ServiceModel;
namespace ListenMusic.Models.Reposistory.Admin
{
    public class AdminUpdateReposistory
    {
        UploadResposistory _upload;
        MusicianReposistory _musician;
        SingerReposistory _singer;
        ListenMusic.Models.Reposistory.User.SongReposistory _songUser;
        ListenMusic.Models.Reposistory.Admin.ManageAlbumReposistory _albumAdmin;
        ManageSongReposistory _manageSong;
        
        public AdminUpdateReposistory()
        {
            _upload = new UploadResposistory();
            _musician = new MusicianReposistory();
            _singer = new SingerReposistory();
            _manageSong = new ManageSongReposistory();
            _songUser = new Models.Reposistory.User.SongReposistory();
            _albumAdmin = new ListenMusic.Models.Reposistory.Admin.ManageAlbumReposistory();
        }
        public bool UpdateSong(HttpPostedFileBase filename,int songid, string songurl, string songname, string lyrics, string singername, string musicianname, int country, int category, string pathSong, string pathVideo)
        {
            try
            {
                if (filename != null)
                {
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        try
                        {

                            int maxMusician = _musician.getMaxID_Musician();
                            int maxSingerID = _singer.getMaxID_Singer();
                            int newSingerID = 0;
                            int newMusicianID = 0;
                            if (singername == "")
                            {
                                newSingerID = 21;
                            }
                            else
                            {
                                ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                                if (singer != null)
                                {
                                    newSingerID = singer.SingerID;
                                }
                                else
                                {
                                    Singer newSinger = new Singer();
                                    newSinger.SingerName = singername;
                                    newSinger.Display = false;
                                    newSinger.CountryID = Convert.ToInt16(country);
                                    newSinger.Picture = "no-image.gif";
                                    //int newSingerID = _singer.getMaxID_Singer();
                                    bool rs = _singer.insertNewSinger(newSinger);
                                    if (rs)
                                    {
                                        newSingerID = maxSingerID + 1;
                                    }
                                    else
                                    {
                                        ts.Dispose();
                                    }
                                }
                            }
                            if (musicianname == "")
                            {
                                newMusicianID = 35;
                            }
                            else
                            {
                                ListenMusic.Musician_ServiceReference.Musician musician = _musician.checkMusicianName(musicianname);
                                if (musician != null)
                                {
                                    newMusicianID = musician.MusicianID;
                                }
                                else
                                {
                                    //int newMusicianID = _musician.getMaxID_Musician();
                                    Musician newMusician = new Musician();
                                    newMusician.MusicianName = musicianname;
                                    bool res = _musician.insertNewMusician(newMusician);
                                    if (res)
                                    {
                                        newMusicianID = maxMusician + 1;
                                    }
                                    else
                                    {
                                        ts.Dispose();
                                    }
                                }
                            }
                            //string[] array_info_user = (string[])Session["user"];
                            string newSongurl = Path.GetFileName(filename.FileName).ToLower().Replace(" ", "-");
                            string fileName = _upload.RandomStr() + "-" + newSongurl;
                            string extension = Path.GetExtension(filename.FileName);
                            string[] ex_audio = new string[] { ".wma", ".mp3" };
                            string[] ex_video = new string[] { ".mp4", ".wmv", ".flv", ".avi", ".mpeg", ".mpg" };
                            List<string> lst_ex_video = new List<string>();
                            List<string> lst_ex_audio = new List<string>();
                            lst_ex_video.AddRange(ex_video);
                            lst_ex_audio.AddRange(ex_audio);
                            string path = "";
                            ListenMusic.AdminSong_ServiceReference.Song s = new ListenMusic.AdminSong_ServiceReference.Song();
                            s.SongID = songid;
                            s.SongName = songname;
                            //s.MemberID = Convert.ToInt32(array_info_user[1]);
                            s.UserName = "vampire89";
                            s.SongURL = fileName;
                            s.DateUp = DateTime.Now;
                            s.CategoryID = category;
                            s.CountryID = Convert.ToInt16(country);
                            s.SingerID = newSingerID;
                            s.MusicianID = newMusicianID;
                            s.Active = false;
                            if (lyrics.Trim() == "")
                            {
                                s.Lyrics = null;
                            }
                            else
                            {
                                s.Lyrics = lyrics;
                            }
                            if (lst_ex_audio.IndexOf(extension) != -1)
                            {
                                s.TypeID = 1;
                                path = pathSong + fileName;
                            }
                            if (lst_ex_video.IndexOf(extension) != -1)
                            {
                                s.TypeID = 2;
                                path = pathVideo + fileName;

                            }
                            bool result = _manageSong.updateSong(s);
                            if (result)
                            {
                                filename.SaveAs(path);
                            }
                            ts.Complete();
                        }
                        catch (Exception ex)
                        {
                            ts.Dispose();
                            return false;
                            throw ex;
                        }
                    }
                }
                else
                {
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        try
                        {
                            int maxMusician = _musician.getMaxID_Musician();
                            int maxSingerID = _singer.getMaxID_Singer();
                            int newSingerID = 0;
                            int newMusicianID = 0;                                                     
                            if (musicianname == "")
                            {
                                newMusicianID = 35;
                            }
                            else
                            {
                                ListenMusic.Musician_ServiceReference.Musician musician = _musician.checkMusicianName(musicianname);
                                if (musician != null)
                                {
                                    newMusicianID = musician.MusicianID;
                                }
                                else
                                {
                                    //int newMusicianID = _musician.getMaxID_Musician();
                                    Musician newMusician = new Musician();
                                    newMusician.MusicianName = musicianname;
                                    bool res = _musician.insertNewMusician(newMusician);
                                    if (res)
                                    {
                                        newMusicianID = maxMusician + 1;
                                    }
                                    else
                                    {
                                        ts.Dispose();
                                    }
                                }
                            }
                            if (singername == "")
                            {
                                newSingerID = 21;
                            }
                            else
                            {
                                ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                                if (singer != null)
                                {
                                    newSingerID = singer.SingerID;
                                }
                                else
                                {
                                    Singer newSinger = new Singer();
                                    newSinger.SingerName = singername;
                                    newSinger.Display = false;
                                    newSinger.CountryID = Convert.ToInt16(country);
                                    newSinger.Picture = "no-image.gif";
                                    //int newSingerID = _singer.getMaxID_Singer();
                                    bool rs = _singer.insertNewSinger(newSinger);
                                    if (rs)
                                    {
                                        newSingerID = maxSingerID + 1;
                                    }
                                    else
                                    {
                                        ts.Dispose();
                                    }
                                }
                            }
                            AdminSong_ServiceReference.Song song = new AdminSong_ServiceReference.Song();
                            song.SongID = songid;
                            song.SongName = songname;
                            song.CountryID = Convert.ToInt16(country);
                            song.CategoryID = category;
                            song.Lyrics = lyrics;
                            song.SingerID = newSingerID;
                            song.MusicianID = newMusicianID;
                            song.SongURL = songurl;
                            song.SingerID2 = null;
                            song.Active = false;
                            string[] ex_audio = new string[] { "wma", "mp3" };
                            string[] ex_video = new string[] { "mp4", "wmv", "flv", "avi", "mpeg", "mpg" };
                            List<string> lst_ex_video = new List<string>();
                            List<string> lst_ex_audio = new List<string>();
                            lst_ex_video.AddRange(ex_video);
                            lst_ex_audio.AddRange(ex_audio);
                            string[] extension = songurl.Split('.');
                            string ex2 = extension[1];
                            if (lst_ex_audio.IndexOf(ex2) != -1)
                            {
                                song.TypeID = 1;                               
                            }
                            if (lst_ex_video.IndexOf(ex2) != -1)
                            {
                                song.TypeID = 2;                               

                            }
                            bool r = _manageSong.updateSong(song);
                            if (r)
                            {
                                ts.Complete();
                                return true;                               
                            }
                            else
                            {
                                ts.Dispose();
                                return false;
                            }
                        
                        }
                        catch (FaultException fe)
                        {
                            ts.Dispose();
                            return false;
                            throw fe;
                        }
                    }                   
                }
                return true;
            }
            catch (FaultException ex)
            {
                return false;
                throw ex;
            }
        }
        public bool UpdateAlbum(int albumid, HttpPostedFileBase filepicture, string oldpicture, string albumname, string singername, string releaseyear, int category, int country, string description, string pathPicture)
        {
            try
            {
                if (filepicture == null)
                {
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        try
                        {
                            AdminAlbum_ServiceReference.Album album = new AdminAlbum_ServiceReference.Album();
                            album.AlbumID = albumid;
                            album.AlbumName = albumname;
                            album.CategoryID = category;
                            album.CountryID = Convert.ToInt16(country);
                            album.Description = description;
                            album.Picture = oldpicture;
                            album.ReleaseYear = Convert.ToInt16(releaseyear);
                            int newSingerID = 0;
                            int maxSingerID = _singer.getMaxID_Singer();
                            if (singername == "")
                            {
                                album.SingerID = 21;
                            }
                            else
                            {
                                ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                                if (singer != null)
                                {
                                    newSingerID = singer.SingerID;
                                }
                                else
                                {
                                    ListenMusic.Singer_ServiceReference.Singer newSinger = new ListenMusic.Singer_ServiceReference.Singer();
                                    newSinger.SingerName = singername;
                                    newSinger.CountryID = Convert.ToInt16(country);
                                    newSinger.Picture = "no-image.gif";
                                    newSinger.Display = false;
                                    bool rs = _singer.insertNewSinger(newSinger);
                                    if (rs)
                                    {
                                        newSingerID = maxSingerID + 1;
                                    }
                                }
                            }
                            album.SingerID = newSingerID;
                            bool result = _albumAdmin.updateAlbum(album);
                            if (result)
                            {
                                ts.Complete();
                            }
                            else
                            {
                                ts.Dispose();
                                return false;
                            }
                        }
                        catch (FaultException e) { return false; throw e; }
                    }
                }
                else
                {
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        string image = Path.GetFileName(filepicture.FileName).ToLower().Replace(" ", "-");
                        string imageName = _upload.RandomStr() + image;
                        string path = pathPicture + imageName;
                        AdminAlbum_ServiceReference.Album album = new AdminAlbum_ServiceReference.Album();
                        album.AlbumID = albumid;
                        album.AlbumName = albumname;
                        album.CategoryID = category;
                        album.CountryID = Convert.ToInt16(country);
                        album.Description = description;
                        album.Picture = imageName;
                        album.ReleaseYear = Convert.ToInt16(releaseyear);
                        int newSingerID = 0;
                        int maxSingerID = _singer.getMaxID_Singer();
                        if (singername == "")
                        {
                            album.SingerID = 21;
                        }
                        else
                        {
                            ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                            if (singer != null)
                            {
                                newSingerID = singer.SingerID;
                            }
                            else
                            {
                                ListenMusic.Singer_ServiceReference.Singer newSinger = new ListenMusic.Singer_ServiceReference.Singer();
                                newSinger.SingerName = singername;
                                newSinger.CountryID = Convert.ToInt16(country);
                                newSinger.Picture = "no-image.gif";
                                newSinger.Display = false;
                                bool rs = _singer.insertNewSinger(newSinger);
                                if (rs)
                                {
                                    newSingerID = maxSingerID + 1;
                                }
                            }
                        }
                        album.SingerID = newSingerID;
                        bool result = _albumAdmin.updateAlbum(album);
                        if (result)
                        {
                            filepicture.SaveAs(path);
                            ts.Complete();
                        }
                        else
                        {
                            ts.Dispose();
                            return false;
                        }
                    }                    
                }
                return true;
            }
            catch (FaultException ex)
            {
                return false;
                throw ex;
            }           
        }
        public bool AddSongInAlbum(HttpPostedFileBase filesong, int albumid, string singername, string songname, int category, int countryid, string musicianname, string pathMP3)
        {
            try {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    try {
                        int maxSongID = _songUser.getMaxID_SongID();
                        int maxMusicianID = _musician.getMaxID_Musician();                        
                        int newMusicianID = 0;
                        string songurl = _upload.RandomStr() + Path.GetFileName(filesong.FileName).ToLower().Trim().Replace(" ","-");
                        string path = pathMP3 + songurl;
                        ListenMusic.User_Song_ServiceReference.Song newSong = new User_Song_ServiceReference.Song();
                        newSong.SongID = maxSongID + 1;
                        newSong.SongName = songname;
                        newSong.SongURL = songurl;
                        newSong.TypeID = 1;
                        newSong.UserName = "vampire89";
                        newSong.Lyrics = null;
                        newSong.Active = false;
                        newSong.CategoryID = category;
                        newSong.CountView = 0;
                        newSong.DateUp = DateTime.Now;
                        newSong.CountryID = Convert.ToInt16(countryid);
                        newSong.SingerID2 = null;
                        if (musicianname == "")
                        {
                            newSong.MusicianID = 35;
                        }
                        else
                        {
                            ListenMusic.Musician_ServiceReference.Musician musician = _musician.checkMusicianName(musicianname);
                            if (musician != null)
                            {
                                newMusicianID = musician.MusicianID;
                            }
                            else
                            {
                                //int newMusicianID = _musician.getMaxID_Musician();
                                Musician newMusician = new Musician();
                                newMusician.MusicianName = musicianname;
                                bool res = _musician.insertNewMusician(newMusician);
                                if (res)
                                {
                                    newMusicianID = maxMusicianID + 1;
                                }
                                else
                                {
                                    ts.Dispose();
                                }
                            }
                        }
                        ListenMusic.Singer_ServiceReference.Singer singer = _singer.checkSingerName(singername);
                        newSong.SingerID = singer.SingerID;
                        newSong.MusicianID = newMusicianID;
                        bool result = _upload.uploadSong(newSong);
                        if (result)
                        {                           
                            ListenMusic.AdminAlbum_ServiceReference.Album_Song albumSong = new AdminAlbum_ServiceReference.Album_Song();
                            albumSong.Album_SongID = _albumAdmin.getMaxID_AlbumSongID() + 1;
                            albumSong.AlbumID = albumid;
                            albumSong.SongID = maxSongID + 1;
                            bool r = _albumAdmin.uploadAlbumSong(albumSong);
                            if (r)
                            {
                                filesong.SaveAs(path);
                            }
                            else
                            {
                                ts.Dispose();
                                return false;
                            }
                        }
                        else
                        {
                            ts.Dispose();
                            return false;
                        }
                        ts.Complete();
                        return true;
                    }
                    catch (FaultException ex) { ts.Dispose(); return false; throw ex; }
                }                
            }
            catch (FaultException e) { return false; throw e; }
        }
    }
}